<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->database();
        $this->load->helper('url');
    }

    public function index() {
        
        $data['logged_in']  = $this->logged_in;
        $data['is_admin']   = $this->is_admin;
        $data['first_name'] = $this->first_name;
        
        //validate form input
        $this->form_validation->set_rules('username', 'User Name', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run() == true) {
            //check to see if the user is logging in
            $username = $this->input->post('username');
            $password = $this->input->post('password');

            $this->load->model('Login_model');
            $ret_val = $this->Login_model->login_user($username, $password);
            if ($ret_val['success']) {
                $session_data = array(
                                        'is_admin'     => $ret_val['is_admin'],
                                        'member_id'    => $ret_val['member_id'],
                                        'first_name'   => $ret_val['first_name'],
                                        'logged_in'    => true
                                      );

                $this->session->set_userdata($session_data);
                redirect($this->config->item('base_url'));  //login is successful
            } else {
                ////if the login was un-successful
                //redirect them back to the login page
                $this->session->set_flashdata('message', $ret_val['message']);
                redirect('login');
            }
        } else {
            //The user is not logging in so display the login page
            //set the flash data error message if there is one
            $data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');

            $data['username'] = array('name' => 'username',
                'id' => '_username',
                'type' => 'text',
                'class' => 'text-box',
                'value' => $this->form_validation->set_value('username'),
            );
            $data['password'] = array('name' => 'password',
                'id' => '_password',
                'type' => 'password',
                'class' => 'text-box'
            );
            $data['additional_javascript_doc_ready_functions'] = '$(".submit-button", ".auth-div").button();$(\'#_username\').focus();';
            $data['title'] = 'Central Alabama VW Club -- Login';
            $data['heading'] = 'Login';
            $this->load->vars($data);
            $this->load->view('login_view', $data);
        }
    }

    //log the user out
    function logout() {
        $this->session->sess_destroy();

        //redirect them back to the page they came from
        redirect('welcome'); 
    }

}

/* End of file login.php */
/* Location: ./application/controllers/login.php */